import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;

import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.muc.DiscussionHistory;
import org.jivesoftware.smackx.muc.MultiUserChat;

public class Output {
	public JFrame loginFrame;
	public JFrame registerFrame;
	public JFrame displayFrame;
	public DisplayPanel displayPanel;
	public RegisterPanel registerPanel;
	public LobbyPanel lobbyPanel;
	public ChatPanel chatPanel;
	public UserPanel userPanel;
	public ArrayList<UserBarPanel> userBarList;
	public CustomListPanel customListPanel;
	public MenuPanelA menuPanelA;
	public LogoPanel logoPanel;
	public CustomInfoPanel customInfoPanel;
	public CustomCreatePanel customCreatePanel;
	public double startTime;
	public int cancelInt;
	public GamePanel gamePanel;
	public boolean customFirst = true, lobbyFirst = true,
			customCreateFirst = true;
	public XMPPConnection myConnection;
	public Map<String, String> myMap;
	public AccountManager myManager;
	public String myServer = "krondor.cs.virginia.edu"; // jabber80.com
	public Input i;
	public ChatManager myChatManager;
	public MultiUserChat muc;
	public String username, rating;

	public Output() {
	}

	// Name, Password1, Password2, EmailField
	public void register(String arg1, String arg2, String arg3, String arg4) {
		// System.out.println(arg1 + " " + arg2 + " " + arg3 + " " + arg4);
		if (arg1.equals("") || arg2.equals("") || arg3.equals(""))
			System.out.println("empty, try again");
		else {
			if (arg2.equals(arg3)) {
				try {
					XMPPConnection tmpC = new XMPPConnection(myServer);
					try {
						tmpC.connect();
					} catch (XMPPException e) {
						System.out
								.println("unable to connect to server, try again later");
					}
					AccountManager tmpA = tmpC.getAccountManager();

					HashMap tmpH = new HashMap<String, String>();
					for (String s : tmpA.getAccountAttributes())
						tmpH.put(s, "");
					tmpH.put("username", arg1);
					tmpH.put("password", arg2);
					tmpH.put("name", "800");
					tmpH.put("email", arg4);

					System.out.println("Attributes: " + tmpH);

					System.out.println("supports account creation: "
							+ tmpA.supportsAccountCreation());

					tmpA.createAccount(arg1, arg2, tmpH);

					System.out.println("Account successfully created.");

					tmpC.disconnect();
				} catch (XMPPException xee) {
					System.out
							.println("Account creation failed, try new username");
					System.out.println(xee);
				}

				login(arg1, arg2);
			} else
				System.out.println("passwords do not match");
		}
	}

	public void login(String arg1, String arg2) {

		startTime = System.currentTimeMillis();

		displayFrame = new JFrame();
		displayFrame.setTitle("Generals");
		displayFrame.setLocation(100, 50);
		displayFrame.setSize(1100, 700);
		displayPanel = new DisplayPanel(this);
		displayFrame.setContentPane(displayPanel);
		displayFrame.setVisible(true);

		// set login-panel invisible
		loginFrame.setVisible(false);
		if (registerFrame != null)
			registerFrame.setVisible(false);

		// build the initial screen

		System.out.println("connecting to server...");
		myConnection = new XMPPConnection("krondor.cs.virginia.edu");
		System.out.println("connected.");
		try {
			System.out.println("logging in...");
			myConnection.connect();
			myConnection.login(arg1 + "@" + myServer, arg2);
			System.out.println("logged in.");
		} catch (XMPPException e) {
			System.out.println(e);
			System.out.println("error incorrect password or username.");
			System.exit(0);
		}

		myManager = myConnection.getAccountManager();

		// Creation of the Input Object to handle Inputs
		i = new Input(this);

		ratingCheck();

		double dtmp = System.currentTimeMillis() - startTime;
		System.out.println("Loadtime: " + dtmp);

		myChatManager = myConnection.getChatManager();

		this.buildLobby();

	}

	public void setID() {
		username = myManager.getAccountAttribute("username");
		rating = myManager.getAccountAttribute("name");
	}

	public void wipe() {
		if (lobbyPanel != null)
			lobbyPanel.setVisible(false);
		if (userPanel != null) {
			userPanel.setVisible(false);
			userPanel.clear();
		}
		if (customListPanel != null)
			customListPanel.setVisible(false);
		if (customInfoPanel != null)
			customInfoPanel.setVisible(false);
		if (customCreatePanel != null)
			customCreatePanel.setVisible(false);

		menuPanelA.a.setText("");
		menuPanelA.b.setText("");
		menuPanelA.c.setText("");
		menuPanelA.d.setText("");
		menuPanelA.f.setText("");

	}

	public void buildLobby() {
		joinChatRoom("hq1");
		
		if (lobbyFirst) {

			logoPanel = new LogoPanel();
			displayPanel.north.add(logoPanel, BorderLayout.EAST);

			lobbyPanel = new LobbyPanel(this);
			displayPanel.center.add(lobbyPanel, BorderLayout.CENTER);

			userPanel = new UserPanel(this);
			displayPanel.east.add(userPanel, BorderLayout.CENTER);

			menuPanelA = new MenuPanelA(this);
			displayPanel.north.add(menuPanelA, BorderLayout.CENTER);
			lobbyFirst = false;
		}

		wipe();

		menuPanelA.a.setText("Custom Games");
		menuPanelA.b.setText("Ladder Games");
		menuPanelA.c.setText("Player Profiles");
		menuPanelA.f.setText("Exit");


		lobbyPanel.clearText();
		lobbyPanel.setVisible(true);
		userPanel.setVisible(true);

		
		userPanel.populate();
	}

	public void joinChatRoom(String s) {
		if (muc != null)
			muc.leave();
		muc = new MultiUserChat(myConnection, s + "@conference." + myServer);
		muc.addMessageListener(i.inputListener);
		
		setID();
		DiscussionHistory history = new DiscussionHistory();
		history.setMaxStanzas(10);
		
		
		try {
			System.out.println("creating room");
			
			muc.create(username + " " + rating);
			muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
			
			System.out.println("room created, sent the form");
		} catch (XMPPException e1) {
			System.out.println(e1);
			System.out.println("room unable to created, trying to join");
			try {
				muc.join(username + " " + rating, "", history,
						SmackConfiguration.getPacketReplyTimeout());
				System.out.println("room joined");

			} catch (XMPPException e2) {
				System.out.println("unable to join room");
			}
		}


	}

	public void ratingCheck() {
		Registration rPacket = new Registration();
		rPacket.setType(IQ.Type.GET);
		myConnection.sendPacket(rPacket);
		i.inputListener.ratingCheck = true;
	}

	public void setRating(int i) {
		myMap.put("name", i + "");

		Registration rPacket = new Registration();
		rPacket.setAttributes(myMap);
		rPacket.setType(IQ.Type.SET);
		myConnection.sendPacket(rPacket);

		System.out.println("rating is set to: " + i);
	}

	public void act(String arg1) {
		if (arg1.equals("Custom Games"))
			custom();
		if (arg1.equals("Ladder Games"))
			ladder();
		if (arg1.equals("Exit"))
			System.exit(0);
		if (arg1.equals("Cancel"))
			cancel();
		if (arg1.equals("Create Game")) {
			customCreate();
			cancelInt = 1;
		}
		if (arg1.equals("Create Game "))
			customCreated();
	}

	//NEEEDSWORK
	public void customCreated() {
		//gamelobby1
		this.joinChatRoom("gl1");
		
		menuPanelA.a.setText("Attack ");
		menuPanelA.b.setText("Auto-Attack ");
		menuPanelA.c.setText("Move ");
		menuPanelA.d.setText("Cards ");
		menuPanelA.f.setText("Finish ");

		if (customListPanel != null)
			customListPanel.setVisible(false);
		if (customCreatePanel != null)
			customCreatePanel.setVisible(false);
		if (customInfoPanel != null)
			customInfoPanel.setVisible(false);

		gamePanel = new GamePanel();
		displayPanel.center.add(gamePanel, BorderLayout.CENTER);

		chatPanel = new ChatPanel();
		displayPanel.center.add(chatPanel, BorderLayout.SOUTH);

	}

	public void customCreate() {
		this.wipe();

		if (customCreateFirst) {
			customCreatePanel = new CustomCreatePanel(this);
			displayPanel.center.add(customCreatePanel, BorderLayout.CENTER);
		}

		customCreatePanel.setVisible(true);
		customInfoPanel.setVisible(true);
		userPanel.setVisible(true);

		menuPanelA.a.setText("Create Game ");
		menuPanelA.b.setText("");
		menuPanelA.c.setText("");
		menuPanelA.f.setText("Cancel");

	}

	public void cancel() {
		if (cancelInt == 0) {
			wipe();
			buildLobby();
		}
		if (cancelInt == 1) {
			wipe();
			custom();
			cancelInt = 0;
		}
	}

	public void custom() {
		wipe();

		if (customFirst) {
			customListPanel = new CustomListPanel();
			displayPanel.center.add(customListPanel, BorderLayout.CENTER);
			customInfoPanel = new CustomInfoPanel(this);
			displayPanel.center.add(customInfoPanel, BorderLayout.SOUTH);
			customFirst = false;
		}

		menuPanelA.a.setText("Join Game");
		menuPanelA.b.setText("Create Game");
		menuPanelA.c.setText("");
		menuPanelA.f.setText("Cancel");

		userPanel.setVisible(true);
		customListPanel.setVisible(true);
		customInfoPanel.setVisible(true);

	}

	public void ladder() {
		System.out.println("Calling Ladder...");
	}
}
